<?php

// +---------------------------------------------+
// |     Copyright  2010 - 2018 InterPhoto       |
// |     http://www.weentech.com                 |
// |     This file may not be redistributed.     |
// +---------------------------------------------+


include('includes/InterPhoto.Core.php');

///////////////////////////////////////////////////////////////////////////// FUNCTIONS /////////////////////////

function GetCategorylinks($categoryid){
	global $siteicategories, $langs;

	$sReturn = ' ' . $langs['nav'] . ' <a href="'.GetUrl('index.php', 'c', $categoryid).'">'.ShortTitle($siteicategories[$categoryid]['title'], 18).'</a>';

	if($siteicategories[$categoryid]['parentid']){
		$sReturn = GetCategorylinks($siteicategories[$categoryid]['parentid']) . $sReturn;
	}

	return $sReturn;
}

///////////////////////////////////////////////////////////////////////////// FUNCTIONS /////////////////////////


$action = ForceIncomingString('action');
$imageid = ForceIncomingInt('id');
$smarty = new InterPhoto;

$showimage = 1;

if($action == 'insertcomment' AND $userinfo['allowcomment'])
{
	$username     = ForceIncomingString('username');
	$content        = ShortTitle(ForceIncomingString('content'), 200);
	$vvckey           = ForceIncomingInt('vvckey');
	$vvc           = ForceIncomingString('vvc');

	if(strlen($username) == 0){
		$errors[] = $sys_langs['please_enter'].$langs['cauthor'];
	}

	if(strlen($content) == 0){
		$errors[] = $sys_langs['please_enter'].$langs['comment'].$langs['content'];
	}

	if(!CheckVVC($vvckey, $vvc)){
		$errors[] = $sys_langs['badvvc'];
	}

	if(isset($errors))
	{
		$comment = array('username' => $username,	 'content' => $_POST['content']);
		$errortitle = $langs['publish'].$langs['comment'].$sys_langs['error'];
	}else{
		$DB->query("INSERT INTO " . TABLE_PREFIX . "comments VALUES (NULL, '$imageid', '$userinfo[allowcommentshow]', '$username', '$content', '".time()."')");

		if(!$userinfo['allowcommentshow'])
		{
			$successtitle = $sys_langs['thanksforcomment'];
			$success = $sys_langs['needadmincheckc'];
		}
	}
}


if(!$userinfo['allowview']) {
	$errors = $sys_langs['nopermissionviewbig'];
	$errortitle = $langs['browse'].$langs['image'].$sys_langs['error'];
	$showimage = 0;
}else{

	$image = $DB->query_first("SELECT i.imageid, i.categoryid, i.userid, i.username, i.sale, i.path, i.filename, i.price, i.title, i.description, i.keywords, i.url, i.viewcount, i.downloadcount, i.created, c.watermarkpos, c.sorting FROM " . TABLE_PREFIX . "images i LEFT JOIN  " . TABLE_PREFIX . "categories c ON (c.categoryid = i.categoryid) WHERE (i.usergroupids = 'all' OR i.usergroupids LIKE '%(".$userinfo['groupid'].")%') AND i.actived = 1 AND c.actived = 1 AND i.imageid = $imageid ");

	if (!$image){
		$errors = $sys_langs['noimage_or_nopermission'];
		$errortitle = $langs['browse'].$langs['image'].$sys_langs['error'];
		$showimage = 0;
	}else{
		if($action != 'insertcomment' AND !IsGet('p')){
			$DB->query("UPDATE " . TABLE_PREFIX . "images SET viewcount = (viewcount + 1) WHERE imageid = '$imageid'");
		}
		$image['keycode'] = md5($image['filename'].WEBSITE_KEY);

		$siteicategories = array();
		$geticategories = $DB->query("SELECT categoryid, parentid, title FROM " . TABLE_PREFIX . "categories WHERE actived = 1 ORDER BY ordernum ASC");
		while($icategory = $DB->getrow($geticategories)){
			$siteicategories[$icategory['categoryid']] = $icategory;
		}

		$categorylinks = GetCategorylinks($image['categoryid']);

		$image_path = BASEPATH.'MyWebsiteImages/'.$image['path'].'_'.WEBSITE_KEY.'/original/'.$image['filename'];
		if (!file_exists($image_path)) {
			$image_path = BASEPATH.'MyWebsiteImages/'.$image['path'].'_'.WEBSITE_KEY.'/760x760/'.$image['filename'];
		}
		$image_size = @getimagesize($image_path);

		$image['width'] = $image_size[0];
		$image['height'] = $image_size[1];
		$image['filesize'] = DisplayFilesize(@filesize($image_path));

		$smarty->assign('image', $image);

		if($image['keywords']){
			$smarty->assign('metakeywords', $image['keywords'] . ','.$mainsettings['siteKeywords']);
			$smarty->assign('metadescription', $image['keywords'] . ',' .$mainsettings['siteMeta']);
		}

		if($image['sorting'] == 'tup')	{
			$prevorderbysql = " ORDER BY imageid DESC ";
			$prevWhere = " imageid < '$imageid' ";
			$nextorderbysql = " ORDER BY imageid ASC ";
			$nextWhere = " imageid > '$imageid' ";
		}else{
			$prevorderbysql = " ORDER BY imageid ASC ";
			$prevWhere = " imageid > '$imageid' ";
			$nextorderbysql = " ORDER BY imageid DESC ";
			$nextWhere = " imageid < '$imageid' ";
		}

		$previmage = $DB->query_first("SELECT imageid FROM " . TABLE_PREFIX . "images WHERE (usergroupids = 'all' OR usergroupids LIKE '%(".$userinfo['groupid'].")%') AND actived = 1 AND categoryid = '$image[categoryid]' AND ".$prevWhere.$prevorderbysql);
		$nextimage = $DB->query_first("SELECT imageid FROM " . TABLE_PREFIX . "images WHERE (usergroupids = 'all' OR usergroupids LIKE '%(".$userinfo['groupid'].")%') AND actived = 1 AND categoryid = '$image[categoryid]' AND ".$nextWhere.$nextorderbysql);

		$smarty->assign('previmageid', $previmage['imageid']);
		$smarty->assign('nextimageid', $nextimage['imageid']);

		if($image['userid']){
			$user = $DB->query_first("SELECT userid, joindate, userfullname, usercompany, useraddress, userpostcode, usertel, userfax, useronline, userwebsite FROM " . TABLE_PREFIX . "users WHERE activated = 1 and userid = '$image[userid]' ");
			if ($user){
				$smarty->assign('user', $user);
			}
		}

		$page = ForceIncomingInt('p', 1);
		$NumPerPage = 10;
		$start = $NumPerPage * ($page-1);

		$getcomments = $DB->query("SELECT * FROM " . TABLE_PREFIX . "comments  WHERE actived = 1 AND imageid = '$imageid' ORDER BY commentid ASC LIMIT $start,$NumPerPage");
		$maxrows = $DB->query_first("SELECT COUNT(commentid) AS value FROM " . TABLE_PREFIX . "comments  WHERE actived = 1 AND imageid = '$imageid' ");
		$comments = $DB->getrows($getcomments);

		$totalpages = ceil($maxrows['value'] / $NumPerPage);

		$smarty->assign('pagelist', Iif($totalpages>1, GetPageList('InterPhoto.php', $totalpages, $page, 10, 'id', $imageid), ''));
		$smarty->assign('comments', $comments);
		$smarty->assign('start', $start);
		$smarty->assign('totalcomments', $maxrows['value']);
	}
}

if($userinfo['allowcomment']){
	header("Expires: Mon, 18 Jul 1988 01:08:08 GMT"); // Date in the past
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
	header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Pragma: no-cache"); // HTTP/1.0

	$smarty->assign('vvckey', CreateVVC());
	$smarty->assign('comment', $comment);
}



$smarty->assign('showimage', $showimage);

//Always needing
$pagenav = '<a href="'.GetUrl('index.php').'">'.$langs['home'].'</a>'
	.$categorylinks
	.Iif($image['imageid'], ' '.$langs['nav'].' <a href="'.GetUrl('InterPhoto.php', 'id', $image['imageid']).'">'.Iif($image['title'], ShortTitle($image['title'], 36), $langs['notitle'].$langs['image']).'</a>');
$smarty->assign('pagenav', $pagenav);
$smarty->assign('userinfo', $userinfo);
$smarty->assign('pagetitle', Iif($image['title'], $image['title'] . ' - ' ) .$mainsettings['siteTitle']);

$smarty->interPlay('interphoto.tpl');

?>